wifi.setmode(wifi.STATION)--工作在ap+客服端，下一步可以通过手机设置密码账号
station_cfg={}
station_cfg.ssid="@PHICOMM_DC"  --这里是你的wifi名字
station_cfg.pwd="123321000" --这里是你的wifi密码
wifi.sta.config(station_cfg)
str1 = string.char(0x00,0x00,0xC4,0x1F,0x48,0x10,0x48,0x10,0xC1,0x1F,0x42,0x10,0x42,0x10,0xC8,0x1F,
0x08,0x00,0xE4,0x3F,0x27,0x25,0x24,0x25,0x24,0x25,0x24,0x25,0xF4,0x7F,0x00,0x00)
str2 = string.char(0x80,0x00,0x00,0x01,0xFC,0x7F,0x44,0x04,0x44,0x04,0xFC,0x3F,0x44,0x04,0x44,0x04,
0xC4,0x07,0x04,0x00,0xF4,0x0F,0x24,0x08,0x42,0x04,0x82,0x03,0x61,0x0C,0x1C,0x70)
str3 = string.char(0x00,0x00,0xE4,0x1F,0x28,0x10,0x28,0x10,0xE1,0x1F,0x22,0x10,0x22,0x10,0xE8,0x1F,
0x88,0x04,0x84,0x04,0x97,0x24,0xA4,0x14,0xC4,0x0C,0x84,0x04,0xF4,0x7F,0x00,0x00)
str4 = string.char(0x80,0x00,0x00,0x01,0xFC,0x7F,0x44,0x04,0x44,0x04,0xFC,0x3F,0x44,0x04,0x44,0x04,
0xC4,0x07,0x04,0x00,0xF4,0x0F,0x24,0x08,0x42,0x04,0x82,0x03,0x61,0x0C,0x1C,0x70)
str5 = string.char(0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x0C,0x00,0x0C,0x00,0x00,0x00,0x0C,0x00,0x0C,0x00,0x00,0x00,0x00,0x00)
str6 = string.char(0x00,0x00,0xFC,0x0F,0x04,0x08,0x04,0x08,0x14,0x0A,0x24,0x0A,0x44,0x09,0x44,0x09,
0x84,0x08,0x84,0x08,0x44,0x09,0x44,0x49,0x24,0x52,0x12,0x52,0x02,0x60,0x01,0x40)
str7 = string.char(0x40,0x00,0x40,0x00,0x40,0x00,0x40,0x00,0xFE,0x1F,0x40,0x10,0x40,0x10,0x40,0x10,
0x40,0x10,0x20,0x10,0x20,0x10,0x10,0x10,0x10,0x10,0x08,0x11,0x04,0x0A,0x02,0x04)

local humidity
local temperature
sda = 3
scl = 4
function init_OLED(sda,scl)
     sla = 0x3c
     i2c.setup(0, sda, scl, i2c.SLOW)
     disp=u8g.ssd1306_128x64_i2c(sla)
     disp:setFont(u8g.font_9x15)
     disp:setFontRefHeightExtendedText()
     disp:setDefaultForegroundColor()
     disp:setFontPosTop()
end
function print_OLED()
    disp:firstPage()
    repeat
    -- disp:drawFrame(0, 0,126,64)
     disp:drawXBM(20,10,16,16,str1)
     disp:drawXBM(20+16,10,16,16,str2)
     disp:drawXBM(20+16*2,10,16,16,str5)
     
     disp:drawXBM(20,10+16,16,16,str3)
     disp:drawXBM(20+16,10+16,16,16,str4)
     disp:drawXBM(20+16*2,10+16,16,16,str5)

     disp:drawStr(20+16*2+8, 13, temperature)
     disp:drawStr(20+16*2+8, 13+16, humidity)
    -- disp:drawStr(0, 40, str1)
    -- disp:drawFrame(2, 16,126,45)
     until disp:nextPage() == false
end
function main()
    print("ll")
    conn=net.createConnection(net.TCP, 0) 
    conn:connect(80,"api.seniverse.com")
    conn:on("receive", function(conn, payload)
    s = string.find(payload,'{') 
    
    payload = string.sub(payload,s,-1)

     a = cjson.decode(payload)  

     v =  a["results"][1].now
    temperature = v.temperature
    feels_like = v.feels_like
    humidity = v.humidity
    visibility = v.visibility
    wind_speed = v.wind_speed
    wind_scale = v.wind_scale
    print_OLED()
   end)
    conn:on("connection", function(conn, payload)

        conn:send("GET  /v3/weather/now.json?key=SdCxrebtW0IXzxMLP&location=jiamusi&language=zh-Hans&unit=c".." HTTP/1.1\r\n".."Host: ".."api.seniverse.com".. "\r\n".."Connection: close\r\n\r\n")

        
    end)
end


init_OLED(sda,scl)
tmr.alarm(1, 1000, tmr.ALARM_AUTO,
--延时函数等待wifi连接       
   function()
            if wifi.sta.getip()== nil then
                print("not connected, Waiting...")
--未连接显示等待
            else
                tmr.stop(1)
                print("Config done, IP is "..wifi.sta.getip())
                conn=net.createConnection(net.TCP, 0) 
                main()
--建立一个连接connection

end
end
)

host = "api.seniverse.com" --天气网站

tmr.alarm(3, 1000*10, tmr.ALARM_AUTO, main) --每10秒到网站获取数据



